//Copyright (c) 2010 Jimmy Merari - jim.merari@gmail.com

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Localization;

using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Text;

using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Tokens;
using DotNetNuke.Framework.Providers;

namespace Cinovasi.Scriptagility
{
    public class ScriptagilityController : DotNetNuke.Entities.Modules.IPortable
    {

        #region IPortable Members

        string DotNetNuke.Entities.Modules.IPortable.ExportModule(int ModuleID)
        {
            ArrayList scripts = ScriptInfo.GetScriptByModuleId(ModuleID);
            List<ScriptInfo> lscript = new List<ScriptInfo>();
            foreach (ScriptInfo info in scripts)
            {
                lscript.Add(info);
            }

            XmlSerializer xmlFormat = new XmlSerializer(typeof(List<ScriptInfo>), new Type[] { typeof(ScriptInfo) });
            StringWriter sw = new StringWriter();
            xmlFormat.Serialize(sw, lscript);
            XmlDocument xmlDoc = new XmlDocument();
            
            xmlDoc.LoadXml(sw.GetStringBuilder().ToString());
            XmlNode xmlNode = xmlDoc.SelectSingleNode("ArrayOfScriptInfo");
            xmlNode.Attributes.Remove(xmlNode.Attributes["xmlns:xsd"]);
            xmlNode.Attributes.Remove(xmlNode.Attributes["xmlns:xsi"]);
            return xmlNode.OuterXml;

        }

        void DotNetNuke.Entities.Modules.IPortable.ImportModule(int ModuleID, string Content, string Version, int UserID)
        {
            StringReader sr = new StringReader(Content);
            try
            {
                XmlSerializer xmlFormat = new XmlSerializer(typeof(List<ScriptInfo>), new Type[] { typeof(ScriptInfo) });

                List<ScriptInfo> imported = (List<ScriptInfo>)xmlFormat.Deserialize(sr);
                if (imported != null)
                {
                    ArrayList existing = ScriptInfo.GetScriptByModuleId(ModuleID);
                    foreach (ScriptInfo si in existing)
                    {
                        ScriptInfo.DeleteScript(si.ScriptId);
                    }

                    foreach (ScriptInfo info in imported)
                    {
                        info.ScriptId = 0;
                        info.ModuleId = ModuleID;
                        info.SaveScript();
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        #endregion

        public static string GetSqlSafeText(string sText)
        {
            sText = sText.Replace("'", "''");
            return sText;
        }

        public static void LogDebug(string msg)
        {
            HttpContext.Current.Trace.Write("SCRIPTAGILITY", msg);
        }
    }
}
